package com.sx.core.utils;

import java.security.MessageDigest;

/**
 * Created by lihao on 2017/7/28.
 */
public class Sha {
    public static String sha(String str){
        String[] hexDigits = { "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
        StringBuffer sb = new StringBuffer();
        try{
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            sha.update(str.getBytes());
            byte[] bytes = sha.digest();
            for (int i = 0; i < bytes.length; i++) {

                int ret = bytes[i];
                if (ret < 0) {
                    ret += 256;
                }
                int m = ret / 16;
                int n = ret % 16;
                String s= hexDigits[m] + hexDigits[n];

                sb.append(s);
            }
        }catch (Exception e){
            return null;
        }
        return sb.toString();
    }
}
